#include "picdialog.h"
#include "ui_picdialog.h"
#include "sandlight.h"
#include "ui_sandlight.h"

picDialog::picDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::picDialog)
{
    ui->setupUi(this);
    setUI(this);
}

picDialog::picDialog(const reflect& N,sandLight *SdNew,QWidget *parent):
    QDialog(parent),
    ui(new Ui::picDialog)
{
    m_N=N,m_SdNew=SdNew;
    ui->setupUi(this);
    setUI(this);
}


picDialog::~picDialog()
{
    if(Pic!=nullptr){
        delete Pic;
        Pic=nullptr;
    }
    if(image!=nullptr){
        delete image;
        image=nullptr;
    }
    delete ui;
}


dealBMP* picDialog::Re_picInfo(){
    return Pic;
}

void picDialog::on_load_Btn_clicked()
{
    QSettings settings("MyCompany", "MyApp"); // 设置注册表路径或配置文件
    QString lastPath = settings.value("LastOpenPath", ".").toString(); // 读取上次路径，默认为当前目录
    QString filepath = QFileDialog::getOpenFileName(nullptr, QStringLiteral("选择图片"), lastPath, "*.bmp");
    if (filepath.trimmed().isEmpty() == false)
    {
        QByteArray ba=filepath.toLocal8Bit();
        char* picPath=ba.data();
        if(Pic!=nullptr){
            delete Pic;
            Pic=nullptr;
        }
        Pic=new dealBMP(picPath);
        if(Pic->pic()!=nullptr){
            if(Pic->picWidth()<m_suitPicW || Pic->picHeight()<m_suitPicH){
                delete Pic,Pic=nullptr;
                QString H=QString::number(m_suitPicH),W=QString::number(m_suitPicW);
                QMessageBox::warning(NULL, "error", "图片宽高过小，宽高像素应为"+W+"*"+H+"像素");
            }
            else{
                if(Pic->picWidth()>m_suitPicW || Pic->picHeight()>m_suitPicH){
                    QString H=QString::number(m_suitPicH),W=QString::number(m_suitPicW);
                    QMessageBox::warning(NULL, "warning", "图片宽高过大，宽高像素应为"+W+"*"+H+"像素");
                }
                load_Pic();
            }
        }
        else{
            QMessageBox::warning(NULL, "warning", "选择的图片需要8位或24位bmp图片");
        }

    }
    if (!filepath.isEmpty()) {
        // 更新保存的路径（只保存文件所在目录）
        settings.setValue("LastOpenPath", QFileInfo(filepath).absolutePath());
    }

}

void picDialog::load_Pic(){
    //-----------------------------------------------------------
    if(Pic->pic()!=nullptr){
        if(m_SdNew!=nullptr){
            m_SdNew->Btn[m_N]->setStyleSheet("color:orange;");
            m_SdNew->ui->recHeight->setEnabled(false);
            m_SdNew->ui->recWidth->setEnabled(false);
            m_SdNew->ui->picReso->setEnabled(false);
            m_SdNew->ui->workBtn->setEnabled(true);
        }
        int width=100;
        int height=int((Pic->picHeight()/double(Pic->picWidth())*width));
        ptr=Pic->ptr(width,height);
        image=new QImage(ptr,width,height,QImage::Format_Grayscale8);
        QSize targetSize=ui->pic_label->size();
        QPixmap pixmap = QPixmap::fromImage(image->scaled(targetSize, Qt::KeepAspectRatio, Qt::FastTransformation));
        //QPixmap pixmap=QPixmap::fromImage(*image);
        ui->pic_label->setPixmap(pixmap);
        ui->clearload_Btn->setEnabled(true);
        ui->saveload_Btn->setEnabled(true);

    }

}

void picDialog::clear_Pic(){
    ui->pic_label->clear();
    ui->clearload_Btn->setEnabled(false);
    ui->saveload_Btn->setEnabled(false);
    if(Pic!=nullptr){
        delete Pic;
        Pic=nullptr;
        if(m_SdNew!=nullptr){
            m_SdNew->ui->recHeight->setEnabled(true);
            m_SdNew->ui->recWidth->setEnabled(true);
            m_SdNew->ui->picReso->setEnabled(true);
            m_SdNew->ui->workBtn->setEnabled(false);
            m_SdNew->Btn[m_N]->setStyleSheet("");
        }
    }
}

void picDialog::on_clearload_Btn_clicked()
{
    clear_Pic();
}

void picDialog::on_saveload_Btn_clicked()
{
    QString fileName;
    fileName=QFileDialog::getSaveFileName(this,tr("Save As"),"",tr("*.bmp"));
    if(fileName.isEmpty()){
        return;
    }
    QByteArray ba=fileName.toLocal8Bit();
    const char* filePath=ba.data();
    BYTE* pic=Pic->pic();
    int picW=Pic->picWidth(),picH=Pic->picHeight();
    int picBit=Pic->bitOut();
    dealBMP outB(filePath,picW,picH);
    bool T=outB.writePic1(pic,picBit);
    if(T){
        QMessageBox::information(this,tr("图片保存"),"Finished!");
    }
    else{
        QMessageBox::critical(this,"错误提示","图片保存失败!");
        return;
    }
}


void delayTime(int k){
    QEventLoop eventloop;
    QTimer::singleShot(50*k,&eventloop,SLOT(quit()));
    eventloop.exec();
}

bool picDialog::reflectWay(long& siteW,long& siteH,reflect picCount,double& c,rflRange* range){
    if(Pic!=nullptr){
        if(siteH<Pic->picHeight() && siteW<Pic->picWidth()
            && siteH>=0 && siteW>=0){
            //在DFF中图片是倒过来的，所以行数也要倒一下
            long Count=(Pic->picHeight()-1-siteH)*Pic->picWidth()+siteW;
            BYTE* P=Pic->pic();
            double q=P[Count]/255.0;
            c=range[picCount].minNum+(range[picCount].maxNum-range[picCount].minNum)*q;
            return true;
        }
        else
            return false;
    }
    else
        return false;
}

void picDialog::closeEvent(QCloseEvent *event) {
    hide();  // 只隐藏窗口，不销毁对象
    event->ignore();
}

void picDialog::setSuitHW(long& picW,long& picH){
    m_suitPicH=picH,m_suitPicW=picW;  //设置适配的图片信息
}
